<?php

namespace App\Http\Service;

use App\Util\Result;
use App\Model\Member;
use App\Model\Operator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

trait ServiceShare{

    //普通账号登录
    public function postLogin($account,$password){
        $member = Member::where('account',$account)->first();
        if(!$member){
            return Result::failure('账号不存在');
        }
        if(Hash::check($password,$member->password)){
            if($member['status']==2){
                return Result::failure('帐号已被管理员禁用');
            }
            session(['member'=>$member]);
            return Result::success('登录成功')->setType(1);
        }
        else{
            return Result::failure('密码错误');
        }
    }

    /**
     * 新增读者
     */
    public function addReaders($data) {
        $model = DB::table('readers');
        $count = $model->where('uid',$data['uid'])->count();
        if (!empty($count)) {
            return Result::failure()->setMsg('读者已存在')->setType(2);
        }
        $res = $model->insert($data);
        if ($res) {
            return Result::success('添加成功')->setType(0);
        } else {
            return Result::failure()->settype(1)->getResult();
        }
    }

    /**
     * 新增柜子
     */
    public function addshelves($data) {
        $model = DB::table('shelves');
        $count = $model->where('number',$data['number'])->count();
        if (!empty($count)) {
            return Result::failure()->setMsg('编号已存在')->setType(2);
        }
        $res = $model->insert($data);
        if ($res) {
            return Result::success('添加成功')->setType(0);
        } else {
            return Result::failure()->settype(1)->getResult();
        }
    }

    /**
     * 当前借阅
     */
    public function nowBorrows($uid,$p,$limit) {
        $model = DB::table('lendlist as a');
        $count = DB::table('lendlist as a')
            ->where('a.ReaderID',$uid)
            ->count();
        $list['pages'] = ceil($count/$limit);
        $list['list'] = $model
            ->where('a.ReaderID',$uid)
            ->leftJoin('Items as b','a.BookID','=','b.SerialNo')
            ->leftJoin('circ_marc as c','b.MarcID','=','c.MarcID')
            ->select('c.Title','c.Author','c.Publisher','c.Pages')
            ->offset($limit*($p-1))
            ->limit($limit)
            ->get();
        return Result::success()->setData($list)->setType(0);
    }

    /**
     * 历史借阅
     */
    public function hadBorrows($uid,$p,$limit) {
        $model = DB::table('lendhist as a');
        $count = DB::table('lendhist as a')
            ->where('a.ReaderID',$uid)
            ->leftJoin('Items as b','a.BookID','=','b.SerialNo')
            ->count();
        $list['pages'] = ceil($count/$limit);
        $list['list'] = $model
            ->where('a.ReaderID',$uid)
            ->leftJoin('Items as b','a.BookID','=','b.SerialNo')
            ->leftJoin('circ_marc as c','b.MarcID','=','c.MarcID')
            ->select('c.Title','c.Author','c.Publisher','c.Pages')
            ->offset($limit*($p-1))
            ->limit($limit)
            ->get();
        return Result::success()->setData($list)->setType(0);
    }

    /**
     * 地图柜子列表
     */
    public function shelfLists($data) {
        $model = DB::table('shelves as a');
        $list['count'] = DB::table('shelves as a')
            ->where('a.longi', '>', $data['left'])
            ->where('a.longi','<',$data['right'])
            ->where('a.lati','>',$data['down'])
            ->where('a.lati','<',$data['up'])
            ->count();
        $list['list'] = $model
            ->where('a.longi', '>', $data['left'])
            ->where('a.longi','<',$data['right'])
            ->where('a.lati','>',$data['down'])
            ->where('a.lati','<',$data['up'])
            ->leftjoin('doors as b','b.sid','=','a.id')
            ->leftjoin('booksindoor as c','c.DoorID','=','b.code')
            ->groupBy('a.id','a.number','a.type','a.name','a.address','a.lati','a.longi')
            ->select(DB::raw('a.id,a.number,a.type,a.name,a.address,a.lati,a.longi,count(c.id) as count'))
            ->get();
        return Result::success()->setData($list)->setType(0);
    }

    /**
     * 图书列表
     */
    public function shelf($sid,$p) {
        $res = DB::table('shelves as a')
            ->where('a.id',$sid)
            ->where('a.type',1)
            ->leftjoin('doors as b','b.sid','=','a.id')
            ->leftjoin('booksindoor as c','c.DoorID','=','b.code')
            ->leftjoin('TagInfo as d','d.rUID','=','c.BookEpc')
            ->whereNotNull('c.BookEpc')
            ->leftjoin('Items as e','e.BarCode','=','d.Barcode')
            ->leftjoin('circ_marc as f','f.MarcID','=','e.MarcID')
            ->select('e.SerialNo as id','f.Title','f.Author','f.Publisher','e.rent_price','f.ImageUrl')
            ->offset(10*($p-1))
            ->limit(10)
            ->get();
        return Result::success()->setData($res)->setType(0);
    }

    public function shelf2($sid) {
        $sel = DB::table('shelves as a')
            ->where('a.id',$sid)
            ->select('a.name','a.number','a.longi','a.lati','a.cid','a.address')
            ->first();
        $sel = get_object_vars($sel);
        $res = DB::table('shelves as a')
            ->where('a.id',$sid)
            ->leftjoin('doors as b','b.sid','=','a.id')
            ->rightjoin('booksindoor as c','c.DoorID','=','b.code');
        $sel['count'] = $res->count();
        $res2 = DB::table('shelves as a')
            ->where('a.id',$sid)
            ->where('c.status',2)
            ->leftjoin('doors as b','b.sid','=','a.id')
            ->rightjoin('flow_records as c','c.did','=','b.id');
        $sel['share_count'] = $res2->count();
        $res3 = DB::table('flow_records as a')
            ->where('c.id',$sid)
            ->where('a.status',2)
            ->leftjoin('doors as b','b.id','=','a.did')
            ->leftjoin('shelves as c','c.id','=','b.sid')
            ->groupBy('a.uid')
            ->select('a.uid')
            ->get();
        $sel['man_count'] = count($res3);
        return Result::success()->setData($sel)->setType(0);
    }

    /**
     * 图书详情
     */
    public function book($bid) {
        $sel = DB::table('Items as a')
            ->where('a.SerialNo',$bid)
            ->leftJoin('circ_marc as b','b.MarcID','=','a.MarcID')
            ->select('b.ImageUrl','b.Title','b.Author','b.Publisher','b.Pages','b.Profile','b.Price','a.sold_price','a.rent_price')
            ->first();
        return Result::success()->setData($sel)->setType(0);
    }

    public function checkLogin(){
        return session('member')?true:false;
    }

    public function getMember(){
        return session('member');
    }

    public function editPwd($password){
        $member = $this->getMember();
        $member->password = bcrypt($password);
        $member->save();
        session(['member'=>$member]);//重新设置session
    }
}


